﻿using AutoMapper;
using PagedList;
using SIG.DAL.Dapper.Model;
using SIG.DAL.Dapper.Repositories;
using SIG.Infrastructure.Configs;
using SIG.Service;
using SIG.ViewModel.Front.Products;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace SIG.WebMVC.Controllers
{
    public class ProductController : BaseController
    {
        private readonly CategoryRepository _categoryService;
        private readonly ProductRepository _productService;
        private readonly TechnologyRepository _technologyService;
        private readonly PageMetaRepository _pageMetaService;
        private readonly IMapper _mapper;

        public ProductController( IMapper mapper)
        {
            _categoryService = new CategoryRepository();
            _productService = new ProductRepository();
            _technologyService = new TechnologyRepository();
            _pageMetaService = new PageMetaRepository();
            _mapper = mapper;

        }

        [Route("product/{seoName?}")]
        // GET: Product
        public ActionResult Index(int? page, string seoName)
        {
           // List<Product> products = new List<Product>();

            var vm = new ProductListVM()
            {
                SeoName = seoName,
                PageIndex = page ?? 1,
                PageSize = SettingsManager.Product.FrontPageSize
            };

            if (!string.IsNullOrEmpty(seoName))
            {
                var category = _categoryService.GetItemBySeoName(seoName);
                if (category == null)
                    return HttpNotFound();

                int count;
                var products = _productService.GetListPagedAcitveItems(vm.PageIndex,vm.PageSize,category.Id, string.Empty, out count);
                vm.TotalCount = count;

                vm.Products = new StaticPagedList<Product>(products, vm.PageIndex, vm.PageSize, vm.TotalCount);

            }
            else
            {
                int totalCount = 0;
                var list = _productService.GetListPagedAcitveItems(vm.PageIndex, vm.PageSize,0,string.Empty, out totalCount);
                vm.TotalCount = totalCount;
         
                vm.Products = new StaticPagedList<Product>(list, vm.PageIndex, vm.PageSize, vm.TotalCount);

            }

            vm.Categories = _categoryService.GetActiveItems().OrderByDescending(m=>m.Importance);
         
            var url = Request.RawUrl;
            vm.PageMeta = _pageMetaService.GetPageMeta(ModelType.MENU, url);

            return View(vm);
         
        }


        //public ActionResult List(int? page, int? categoryId, string keyword)
        //{
        //    var vm = new ProductListVM()
        //    {
        //        CategoryId = categoryId ?? 0,
        //        PageIndex = (page ?? 1) - 1,
        //        PageSize = SettingsManager.Product.FrontPageSize
        //    };
        //    int totalCount = 0;
        //    var list = _productService.GetActivePagedElements(vm.PageIndex, vm.PageSize, string.Empty, vm.CategoryId, out totalCount);
        //    var dto = _mapper.Map<List<Product>, List<ProductVM>>(list);

        //    vm.TotalCount = totalCount;

        //    var categoryList = _categoryService.GetAll().Where(m => m.Active).OrderByDescending(c => c.Importance).ToList();
        //    vm.Categories = _mapper.Map<List<Category>, List<CategoryVM>>(categoryList);
        //    vm.Products = new StaticPagedList<ProductVM>(dto, vm.PageIndex + 1, vm.PageSize, vm.TotalCount);

        //    return View(vm);

        //}

        public ActionResult Detail(int id)
        {
            var product =  _productService.GetById(id);
            if (product == null)
            {
                return HttpNotFound();
            }

            product.ViewCount += 1;
            _productService.Update(product);
            // var vm = _mapper.Map<ProductDetail>(product);
            ProductDetail vm = new ProductDetail
            {
                Id=product.Id,
                ProductName = product.ProductName,
                ProductNo = product.ProductNo,
                Body = product.Body,
                Description = product.Description,
                ViewCount = product.ViewCount
            };

            if (!string.IsNullOrEmpty(product.ImageUrl))
            {
                vm.Photos = product.ImageUrl.Split('|');
            }

          

            var pageMeta = _pageMetaService.GetPageMeta(ModelType.PRODUCT, product.Id.ToString());
            if (pageMeta != null)
            {
                vm.Title = pageMeta.Title;
                vm.Keywords = pageMeta.Keyword;
                vm.SEODescription = pageMeta.Description;
            }

            var listCount = _technologyService.GetCountByParentId(id);
            vm.HasTech = listCount>0;

            return View(vm);
        }

        public ActionResult Technology(int id, int? techId)
        {
            PageTechnologyList vm = new PageTechnologyList();
            vm.Technologies = _technologyService.GetListPagedAcitveItems(1,int.MaxValue,id);
            if (techId > 0)
            {
                vm.Technology = vm.Technologies.Where(m => m.Id == techId).FirstOrDefault();
            }
            else
            {
                vm.Technology = vm.Technologies.FirstOrDefault();
            }

            vm.PageMeta = _pageMetaService.GetPageMeta(ModelType.TECHNOLOGY, vm.Technology.Id.ToString());

            return View(vm);
        }

        public PartialViewResult RecommendProducts(int count)
        {
            int reCount;
            var products = _productService.GetListPagedRecommendItems(1,6,0,string.Empty,out reCount);
          //  var aa = count;
          //  var vm = _mapper.Map<List<Product>, List<ProductVM>>(products);
            return PartialView("_RecommendProducts", products);
        }


    }
}